package com.btf.push;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import com.butterfly.vv.model.Contact;
import com.butterfly.vv.vv.utils.Debug;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
public class SchoolVcardListener implements PacketListener {
	
	Handler handler = null;
	
	public SchoolVcardListener(Handler handler) {
		// TODO Auto-generated constructor stub
		this.handler = handler;
	}

	@Override
	public void processPacket(Packet arg0) {
		// TODO Auto-generated method stub
		System.out.println("--*SchoolVcardListener"+arg0.toXML());
		if (arg0 instanceof SchoolVCard) {
			SchoolVCard gpsPacket =  (SchoolVCard) arg0;
			System.err.println(""+gpsPacket.getRosterItemCount()+gpsPacket.toXML());
			Collection<com.btf.push.SchoolVCard.Item> items = gpsPacket.getRosterItems();
			ArrayList<com.beem.project.beem.service.Contact> contacts = new ArrayList<com.beem.project.beem.service.Contact>();
    		ArrayList<com.teleca.jamendo.api.Contact> vvcontacts = new ArrayList<com.teleca.jamendo.api.Contact>();

			for (Iterator iterator = items.iterator(); iterator.hasNext();) {
				com.btf.push.SchoolVCard.Item item = (com.btf.push.SchoolVCard.Item) iterator.next();
				com.beem.project.beem.service.Contact contact = new com.beem.project.beem.service.Contact(item.getUser());
				contact.setBday(item.getBday());
				contact.setRole(item.getRole());
				contact.setName(item.getUser());
//				contact.setPhoto(StringUtils.encodeBase64(item.getPhoto()));
				
				com.teleca.jamendo.api.Contact c = new com.teleca.jamendo.api.Contact();
				c.setBday(item.getBday());
				c.setRole(item.getRole());
				c.setmName(item.getUser());
//				c.setPhoto(StringUtils.encodeBase64(item.getPhoto()));
				vvcontacts.add(c);

				Debug.getDebugInstance().log("processPacket "+item.toXML()+" "+item.getBday()+item.getRole()+item.getPhoto());
				//contact.setDistance(item.getDistance());
				contacts.add(contact);
			}
			Message msg = new Message();
			Bundle data = new Bundle();
			
			data.putParcelableArrayList("schoolvvcontacts",  contacts);
			msg.setData(data);
			msg.what = 2;
			handler.sendMessage(msg);
//			for (Iterator iterator = items.iterator(); iterator
//					.hasNext();) {
//				com.btf.push.GPSPacket.Item item = (com.btf.push.GPSPacket.Item) iterator.next();
//			}
		}
	}

}
